#!/usr/bin/env python
# coding=utf-8
import akshare as ak
import pandas as pd
import numpy as np
from include import dataBase

class dealDate:
    def __init__(self,connect):
        ''' 采用一个数据库连接进行初始化  '''
        self.dataBaseConnect = connect
        self.baseName = "dateX"

    def saveDate(self):
        tradeDate = ak.tool_trade_date_hist_sina()
        
        sql = '''CREATE TABLE if not EXISTS dateX (dayNumber INT PRIMARY KEY,tradeDate DATE NOT NULL,UNIQUE (tradeDate));'''
        ret = self.dataBaseConnect.create(self.baseName,sql)

        for date in tradeDate.itertuples():
            datesql = ''' insert into dateX values({},"{}")ON DUPLICATE KEY UPDATE dayNumber = dayNumber;'''.format(date[0],date[1])
            self.dataBaseConnect.select(self.baseName,datesql)
        self.dataBaseConnect.commit()
        print("保存日期成功")


    def getAllDate(self):
        sql = ''' select tradeDate from dateX  '''
        return self.dataBaseConnect.select(self.baseName,sql)
        
    def getNewestDate(self,tableName):
        ''' 获取当前表中的最新时间  '''
        return self.dataBaseConnect.selectMax(tableName,"date")

    def getOldestDate(self,tableName):
        ''' 获取当前表中的最早时间  '''
        return self.dataBaseConnect,selectMin(tableName,"date")


    def getPartDate(self,beginDate,endDate):
        ''' 时间格式："2023-01-01"  '''
        sql = ''' select tradeDate from dateX where tradeDate between '{}' and '{}'; '''.format(beginDate,endDate)
        return self.dataBaseConnect.select(self.baseName,sql)




